package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelConcern;
import cn.wolfcode.trip.base.mapper.TravelConcernMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelConcernQueryObject;
import cn.wolfcode.trip.base.service.ITravelConcernService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelConcernServiceImpl implements ITravelConcernService{
    @Autowired
    private TravelConcernMapper travelConcernMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelConcernMapper concernMapper;

    //============================小智=========================================

    @Override

    public PageInfo<TravelConcern> query(TravelConcernQueryObject qo) {
        List<Long> travelIds = travelMapper.selectByUserId(qo.getUserId());
        qo.setTravelId(travelIds);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<TravelConcern> list = concernMapper.selectByTravelId(qo);
        return new PageInfo<TravelConcern>(list);
    }

    @Override
    public void updateIsViewed(List<Long> userId) {
        concernMapper.updateIsViewed(userId);
    }

    @Override
    public int selectNotLike(Long userId) {
        List<Long> travelIds = travelMapper.selectByUserId(userId);
        int travel=concernMapper.selectByTravelIdLike(travelIds);
        return travel;
    }

    //===============================小智=======================================

    @Override
    //游记收藏记录新增
    public void saveCollector(TravelConcern tc) {

        //额外判断
        List<TravelConcern> list = travelConcernMapper.searchNull();
        for (TravelConcern concern : list) {
            travelConcernMapper.deleteByPrimaryKey(concern.getId());
        }

        //判断是否有点赞操作
        //注意这里传递点赞者的id 但SQL执行的是收藏者的id
        TravelConcern temp = travelConcernMapper.checkUserTravelConcerns(tc.getCollector().getId(),tc.getTravel().getId());
        if(temp==null){
            //默认点赞消息没有查看
            tc.setViewed(false);
            tc.setCollectDate(new Date());
            travelConcernMapper.insert(tc);
        }else{
            //如果用于游记表中有收藏记录 直接更新其点赞字段
            tc.setId(temp.getId());
            tc.setCollectDate(new Date());
            travelConcernMapper.updateCollectorByPrimaryKey(tc);
        }

    }

    @Override
    //当前用户游记收藏记录的修改
    public void updateCollector(TravelConcern travelConcern) {
        TravelConcern tc = travelConcernMapper.checkUserConcernCollector(travelConcern.getCollector().getId(), travelConcern.getTravel().getId());
        travelConcern.setId(tc.getId());
        travelConcern.setCollector(null);
        travelConcern.setCollectDate(null);
        travelConcernMapper.updateCollectorByPrimaryKey(travelConcern);
    }


    @Override
    //当前用户游记点评的修改
    public void update(TravelConcern travelConcern) {
        TravelConcern tc = travelConcernMapper.checkUserTravelConcerns(travelConcern.getLiker().getId(), travelConcern.getTravel().getId());
        travelConcern.setId(tc.getId());
        travelConcern.setLiker(null);
        travelConcern.setLikeDate(null);
        travelConcernMapper.updateLikerByPrimaryKey(travelConcern);
    }

    @Override
    public void checkUserConcernCollector(Long userId, Long travelId) {
        //如果点评对象存在表示用户已经对游记进行收藏
        TravelConcern tc = travelConcernMapper.checkUserConcernCollector(userId,travelId);
        if(tc==null){
            throw new RuntimeException("该用户没有对游记收藏");
        }
    }




    @Override
    //判断当前登录用户是否已经对某游记点赞
    public void checkUserTravelConcerns(Long userId, Long travelId) {
        //如果存在游记 表示当前用户已经对游记进行点赞
        TravelConcern tc = travelConcernMapper.checkUserTravelConcerns(userId,travelId);
        if(tc==null){
            throw new RuntimeException("该用户没有对游记点赞");
        }
    }

    @Override
    //游记点赞记录新增
    public void save(TravelConcern travelConcern) {

        //额外判断
        List<TravelConcern> list = travelConcernMapper.searchNull();
        for (TravelConcern concern : list) {
            travelConcernMapper.deleteByPrimaryKey(concern.getId());
        }

        //判断是否有收藏操作
        //注意这里传递点赞者的id 但SQL执行的是收藏者的id
        TravelConcern tc = travelConcernMapper.checkCollectorTravelConcerns(travelConcern.getLiker().getId(),travelConcern.getTravel().getId());
        //判断用户对该游记继续点赞和收藏操作
        //如果为空表示用户不存在表中 执行插入操作
        if(tc==null){
            //默认点赞消息没有查看
            travelConcern.setViewed(false);
            travelConcern.setLikeDate(new Date());
            travelConcernMapper.insert(travelConcern);
        }else{
            //如果用于游记表中有收藏记录 直接更新其点赞字段
            travelConcern.setId(tc.getId());
            travelConcern.setLikeDate(new Date());
            travelConcernMapper.updateLikerByPrimaryKey(travelConcern);
        }

    }

    @Override
    //游记点赞次数
    public int getLikeNumsByTravelId(Long travelId) {
        return travelConcernMapper.getLikeNumsByTravelId(travelId);
    }

    @Override
    //游记收藏次数
    public int getcollectorNumsByTravelId(Long travelId) {
        return travelConcernMapper.getcollectorNumsByTravelId(travelId);
    }


}
